﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var cookie = Request.Cookies["wl_auth"];
            if (cookie == null)
            {
                AccessTokenLabel.Text = "No wl_auth cookie";
                return;
            }

            string accessToken = cookie.Values["access_token"];
            if (string.IsNullOrEmpty(accessToken))
            {
                AccessTokenLabel.Text = "No access token";
                return;
            }

            AccessTokenLabel.Text = HttpUtility.HtmlEncode(HttpUtility.UrlDecode(accessToken));

            try
            {
                using (var client = new WebClient())
                {
                    string result = client.DownloadString("https://apis.live.net/v5.0/me?access_token=" + accessToken);
                    BasicInfoLabel.Text = HttpUtility.HtmlDecode(result);
                }
            }
            catch (WebException ex)
            {
                BasicInfoLabel.Text = ex.Message;
            }
        }
    }
}
